<?php
class Picture_model extends CI_Model {

	function img() {
		$username = $this->session->userdata('username');
		$password = $this->session->userdata('password');
		$id = $this->input->post('q');
		
	//	$url = "http://localhost:8080/moms-diary/services/picture/galleries";
		$url = "http://localhost:8080/moms-diary/".$id;
		
		$auth = $username.":".$password;
	
		$context = stream_context_create(array(
				'http' => array(
						'header'  => "Authorization: Basic " . base64_encode($auth)
				)
		));
		$data = file_get_contents($url, false, $context);
		if(isset($data)) {
			echo $data;
		}
	}
	
	function getPictures() {
		$username = $this->session->userdata('username');
		$password = $this->session->userdata('password');

		$url = "http://localhost:8080/moms-diary/services/picture/galleries";

		$auth = $username.":".$password;

		$context = stream_context_create(array(
		    'http' => array(
			'header'  => "Authorization: Basic " . base64_encode($auth)
			)
		));
		$data = file_get_contents($url, false, $context);
		if(isset($data)) {
			echo $data;
		}
	}

	function saveUpload() {
		$username = $this->session->userdata('username');
		$password = $this->session->userdata('password');
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$description = $this->input->post('description');

		$url_ = explode("/", $id);

		$url = "http://localhost:8080/moms-diary/services/picture/gallery/".$url_[3];
		//$username="rm.herwidodo@babytivities.com";
		//$password="e8HSG6Mlj>";

		$postFields = array();

		//files
		$postFields['title'] = $title;
		$postFields['description'] = $description;

		$auth = $username.":".$password;

		$this->load->library('curl');
		$this->curl->create($url);
		$this->curl->options(array(
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_USERPWD => $auth, 
					CURLOPT_POST => true, 
					CURLOPT_HTTPHEADER => array("Content-type: application/x-www-form-urlencoded"), 
					CURLOPT_POSTFIELDS => $postFields
				));

		//execute the API Call
		$returned_data = $this->curl->execute();

		echo $returned_data;
	}
	
	function getUpload(){
		$username = $this->session->userdata('username');
		$password = $this->session->userdata('password');
		$filepath = $_FILES['mypicture']['tmp_name'][0];
		
		$url = "http://localhost:8080/moms-diary/services/picture/gallery";
		//$username="rm.herwidodo@babytivities.com";
		//$password="e8HSG6Mlj>";
		
		$postFields = array();
		
		//files
		$postFields['file'] = "@".$filepath;
		
		$auth = $username.":".$password;
		
		$curl_handle = curl_init($url);
		
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl_handle, CURLOPT_USERPWD, $auth);
		curl_setopt($curl_handle, CURLOPT_POST, true);
		curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
		curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postFields);
		
		//execute the API Call
		$returned_data = curl_exec($curl_handle);
		$return =  curl_getinfo($curl_handle);
		
		echo $returned_data;
		
	}
}
